#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/17 10:19
# @Author  : Yunhao.Cao
# @File    : main.py
from __future__ import absolute_import, unicode_literals
from bs4 import BeautifulSoup
from time import sleep
from selenium import webdriver

__author__ = 'Yunhao.Cao'

__ALL__ = []


def _main():
    """
    解析页面：https://item.jd.com/3597549.html

    需要配置PhantomJS环境变量到 'PATH' 或者 配置 'executable_path' 参数
    :return:
    """
    driver = webdriver.PhantomJS(executable_path="phantomjs")
    print("get...")
    driver.get("https://item.jd.com/3597549.html")
    print("sleep...")
    sleep(5)

    text = driver.page_source

    soup = BeautifulSoup(text, "html.parser")

    try:
        # 店铺名
        shop_name = soup.select_one("div.name a").get("title", None)
        print "shop name = {}".format(shop_name)

        # price
        price_text = soup.select_one("span.p-price span[class^=\'price J-p-\']").text
        print "good price = {}".format(price_text)

        # 评论数目
        comment_number_text = soup.select_one(
            "#detail > div.tab-main.large > ul > li[data-anchor=\'#comment\'] > s").text.replace("(", "").replace(")",
                                                                                                                  "")
        print "number of comment = {}".format(comment_number_text)

    except Exception as e:
        print("解析失败. e = {}".format(e))


if __name__ == '__main__':
    _main()
